package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.SeckillFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class SeckillGoodsController {

    @Autowired
    private SeckillFeignClient seckillFeignClient;


    /**
     * 秒杀下单确认
     * @param model
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String trade(Model model){
        //获取确认页信息数据
        Result<Map<String,Object>> result = seckillFeignClient.trade();
        //判断状态
        if(result.isOk()){
            //设置数据
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else {

            return "seckill/fail";
        }


    }

    /**
     * 跳转到抢购页面
     * @param request
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String queue(HttpServletRequest request){

        //获取参数
        String skuId = request.getParameter("skuId");
        String skuIdStr = request.getParameter("skuIdStr");
        //存储
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);


        return "seckill/queue";
    }


    /**
     * 获取秒杀商品详情
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String item(@PathVariable  Long skuId, Model model){

        Result result = seckillFeignClient.getSeckillGoods(skuId);

        model.addAttribute("item",result.getData());

        return "seckill/item";
    }

    /**
     * 秒杀列表
     * @return
     */
    @GetMapping("/seckill.html")
    public String index(Model model){

        Result<List> result = seckillFeignClient.findAll();

        model.addAttribute("list",result.getData());
        return "seckill/index";
    }
}
